package com.project.nimsen.weixin.utils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.project.nimsen.weixin.bean.BaseMsg;
import com.project.nimsen.weixin.bean.TextRMsg;
import com.project.nimsen.weixin.common.WeiXinConstant;

public class WXMessageUtils {
	public static BaseMsg xml2Msg(HttpServletRequest request)throws Exception{
		request.setCharacterEncoding("UTF-8");
	    ServletInputStream in = request.getInputStream();
	    SAXReader reader = new SAXReader();
	    Document doc = reader.read(in);
	    
	    //开始转换
	    Element root = doc.getRootElement();
	    String msgType = root.elementText("MsgType");
	    BaseMsg msg = null;
	    
	    //判断是消息还是事件
	    if(WeiXinConstant.WEIXIN_MSGTYPE_EVENT.equals(msgType)){
	    	
	    }else{
	    	if(WeiXinConstant.WEIXIN_MSGTYPE_IMAGE.equals(msgType)){
	    		
	    	}else if(WeiXinConstant.WEIXIN_MSGTYPE_LOCATION.equals(msgType)){
	    		
	    	}else if(WeiXinConstant.WEIXIN_MSGTYPE_VOICE.equals(msgType)){
	    		
	    	}else if(WeiXinConstant.WEIXIN_MSGTYPE_VIDEO.equals(msgType)){
	    		
	    	}else if(WeiXinConstant.WEIXIN_MSGTYPE_LINK.equals(msgType)){
	    		
	    	}else{
	    		//WEIXIN_MSGTYPE_TEXT默认为文本信息
	    		msg = new TextRMsg();
	    		msg.fullProperty(root);
	    	}
	    }
		return msg;
	}
}
